// Load data

clear
import excel data_reflection_horp.xlsx, sheet("Data") firstrow


// Aggregate data by subject for each treatment and risk order

forval i= 2/4 {
	foreach treat in "loss" "gain" "sprg" {
		quietly egen `treat'`i' = rowtotal(`treat'`i'_*) if `treat'2_a ~= .
	}
}


// Calculate differences between original and replication data

quietly gen replication = 0
quietly replace replication = 1 if session >= 12

forval i= 2/4 {
	foreach treat in "loss" "gain" "sprg" {
		quietly sum `treat'`i' if replication == 0
		local `treat'`i'freq = r(mean)/12
		quietly sum `treat'`i' if replication == 1
		display abs(r(mean)/12-``treat'`i'freq')
	}
}


// Mann Whitney U tests between original and replication data

forval i= 2/4 {
	foreach treat in "loss" "gain" "sprg" {
		ranksum `treat'`i', by(replication)
	}
}


// Ordered probits to test for effects of background variables

quietly reshape long loss gain sprg, i(id) j(order)
quietly rename loss apportionment1
quietly rename gain apportionment2
quietly rename sprg apportionment3
quietly reshape long apportionment, i(id order) j(treatment)

quietly gen loss = 1 if treatment == 1
quietly gen gain = 1 if treatment  == 2
quietly gen sp = 1 if treatment == 3
quietly replace loss = 0 if loss == .
quietly replace gain = 0 if gain == .
quietly replace sp = 0 if sp == .

quietly gen male = 1 if gender == "M"
quietly replace male = 0 if gender == "F" & gender ~= "O"

quietly gen dutch = 1 if nationality == "Dutch"
quietly replace dutch = 0 if nationality ~= "Dutch"
quietly replace dutch = . if nationality == "" || nationality == "Other"

quietly gen econ = 1 if major == "Economics"
quietly replace econ = 0 if major ~= "Economics" & major ~= ""

quietly gen income1000 = 0 if income == "1000"
quietly replace income1000 = 1 if income ~= "1000" & income ~= "other" & income ~= ""

oprobit apportionment i.loss i.sp i.male age i.dutch i.econ i.income1000 if order == 2, cluster(id)
margins, predict(outcome(#13)) dydx(*)

oprobit apportionment i.loss i.sp i.male age i.dutch i.econ i.income1000 if order == 3, cluster(id)
margins, predict(outcome(#13)) dydx(*)

oprobit apportionment i.loss i.sp i.male age i.dutch i.econ i.income1000 if order == 4, cluster(id)
margins, predict(outcome(#13)) dydx(*)

oprobit apportionment i.loss i.sp i.male i.male#i.loss i.male#i.sp age i.dutch i.econ i.income1000 if order == 2, cluster(id)
margins, predict(outcome(#13)) dydx(*)

oprobit apportionment i.loss i.sp i.male i.male#i.loss i.male#i.sp age i.dutch i.econ i.income1000 if order == 4, cluster(id)
margins, predict(outcome(#13)) dydx(*)


// ANOVA to test for ordering effects

quietly gen firstblockloss = 1 if substr(blockorder,1,4) == "loss"
quietly replace firstblockloss = 0 if firstblockloss == .

forval i= 2/4 {
	quietly gen loss`i'pos = (strpos(blockorder,"loss`i'")-1)/6
	quietly gen g`i'pos = (strpos(blockorder,"gain`i'")-1)/6 if strpos(blockorder,"gain`i'")~=0
	quietly replace g`i'pos = (strpos(blockorder,"sprg`i'")-1)/6 if strpos(blockorder,"sprg`i'")~=0
}

quietly gen lossfirst = .
forval i= 2/4 {
	quietly replace lossfirst = 1 if order == `i' & loss`i'pos<g`i'pos
	quietly replace lossfirst = 0 if order == `i' & loss`i'pos>g`i'pos
}

quietly gen position = .
forval i= 2/4 {
	quietly replace position = loss`i'pos if order == `i' & loss == 1
	quietly replace position = g`i'pos if order == `i' & gain == 1 
	quietly replace position = g`i'pos if order == `i' & sp == 1
}

quietly gen riskaversion = 1 if order == 2
quietly replace riskaversion = 0 if order ~= 2
quietly gen prudence =1 if order == 3
quietly replace prudence = 0 if order ~= 3
quietly gen temperance = 1 if order == 4
quietly replace temperance = 0 if order ~= 4

anova apportionment riskaversion prudence loss sp lossfirst firstblockloss position


// Kruskal-Wallis tests for session effects

clear
import excel data_reflection_horp.xlsx, sheet("Data") firstrow

forval i= 2/4 {
	foreach treat in "loss" "gain" "sprg" {
		quietly egen `treat'`i' = rowtotal(`treat'`i'_*) if `treat'2_a ~= .
	}
}

forval i= 2/4 {
	foreach treat in "loss" "gain" "sprg" {
		kwallis `treat'`i', by(session)
	}
}
